package com.afd.ozblip.activity;

import java.sql.SQLException;
import java.util.List;

import com.afd.ozblip.adapter.BusinessListAdapter;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.util.CollectionUtil;
import com.j256.ormlite.dao.Dao;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class HistoryActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private ListView topListView;
	private TextView tipTv;
	private LinearLayout commonRight;
	/****************** 其它变量 **************************/
	private List<BusinessEntity> businessTopList;
	private BusinessListAdapter businessListAdapter;
	private OzBlipApplication myApp;
	private Dao<BusinessEntity, Integer> businessDao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 获取列表数据
		requestTopListData();

	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.topListView = (ListView) findViewById(R.id.lv_top_businessList);
		this.tipTv = (TextView) findViewById(R.id.tip_text_noData);
		this.commonRight = (LinearLayout) findViewById(R.id.ll_common_right);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {

		View.OnClickListener historyClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_common_right:
					Dialog alertDialog = new AlertDialog.Builder(
							HistoryActivity.this)
							.setTitle("提示")
							.setMessage("确定删除浏览历史吗？")
							.setIcon(android.R.drawable.ic_menu_help)
							.setPositiveButton("确定",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											try {
												businessDao.deleteBuilder().delete();
											} catch (Exception e) {
												e.printStackTrace();
											}
											finish();
										}
									})
							.setNegativeButton("取消",
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.dismiss();
										}
									}).create();
					alertDialog.show();
					break;

				default:
					break;
				}
			}
		};
		this.commonRight.setOnClickListener(historyClick);
		this.topListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> itemView, View arg1,
					int parameInt, long arg3) {
				Intent businessIntent = new Intent(HistoryActivity.this,
						BusinessInfoActivity.class);
				BusinessEntity business = HistoryActivity.this.businessTopList
						.get(parameInt);
				businessIntent.putExtra("businessInfo", business);
				startActivity(businessIntent);
			}
		});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	/**
	 * 获取评论列表数据
	 */
	private void requestTopListData() {

		myApp = (OzBlipApplication) getApplication();
		try {
			businessDao = myApp.getDBHelper().getDao(BusinessEntity.class);
			businessTopList = businessDao.queryForAll();
		} catch (Exception e) {
			e.printStackTrace();
		}

		setTopListAdapter();
	}

	/**
	 * 调用评论列表适配器
	 */
	public void setTopListAdapter() {
		if (CollectionUtil.UnNullAndZero(businessTopList)) {
			this.tipTv.setVisibility(8);
			this.businessListAdapter = new BusinessListAdapter(
					HistoryActivity.this);
			this.businessListAdapter.addItems(businessTopList);
			this.topListView.setAdapter(this.businessListAdapter);
		} else {
			this.tipTv.setVisibility(0);
			this.tipTv.setText(getResources().getString(R.string.noData_text));
		}
	}
}